home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8316 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Pointer to member fctn within member fctn
  5. Date: 16 Feb 1996 19:43:15 GMT
  6. Organization: Borland International
  7. Message-ID: <4g2moj$f81@druid.borland.com>
  8. References: <4fgab1$h27@newsserv.zdv.uni-tuebingen.de>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4fgab1$h27@newsserv.zdv.uni-tuebingen.de>, 
  15. hans.loeffler@student.uni-tuebingen.de says...
  16. >
  17. >I have a problem calling a member function through a pointer within
  18. >another member function
  19. >Consider:
  20. >
  21. >typedef void (*pmfctn)(int); //pointer to member function
  22. >
  23.  
  24. Unfortunately, the compiler didn't read your comment, so it didn't know that 
  25. you meant pmfctn to be a pointer to member function. It read the actual 
  26. declaration, and figured that pmfctn is a pointer to an ordinary function that 
  27. takes an int and returns void. If you change the typedef to actually define a 
  28. pointer to member function it will work much better.
  29.  
  30. class X;
  31. typedef void (X::*pmfctn)(int); // pointer to member function of X.
  32.  
  33. >class X {
  34. >void f1(int);
  35. >void f2(int);
  36. >
  37. >pmfctn pf1, pf2;
  38. >
  39. >void f3(pmfctn);
  40. >};
  41. >
  42. >void X::f1(void) {
  43. >        // do something
  44. >        ...
  45. >        return;
  46. >}
  47. >...
  48. >
  49. >void X::f3(pmfctn pf) {
  50. >
  51. >int a = 5;
  52. >// I want to call f1 or f2 now
  53. >// problem here:
  54. >this->*pf(a);  // **
  55. >}
  56.  
  57. This should not compile, even with the correct definition of pmfctn. It needs 
  58. an additional set of parentheses:
  59.  
  60. (this->*pf)(a);
  61.  
  62. As originally written, it says to call pf(a) and use the result as the 
  63. right-hand operand of ->*.
  64.  
  65. > //  ** this results in a runtime error with Borland C++ V4.5, when
  66. >calling f3(pf1) or f3(pf2) 
  67. >// I initialize pf1 as:
  68. >pf1 = &X::f1;
  69. >
  70. >In books I only saw things like
  71. >
  72. >X *px;
  73. >
  74. >px->*pf1(a);
  75. >which call the functions from an object and not within a member
  76. >function
  77. >
  78. >What is wrong with my solution?
  79. >
  80. >Hans
  81. >
  82.  
  83.